package org.ecype.diego;

import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public class Cursors {

	private Cursors() {
	}

	public static ContentValues toContentValues(Cursor cursor) {
		ContentValues values = new ContentValues(cursor.getColumnCount());
		for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
			values.put(cursor.getColumnName(columnIndex), cursor.getString(columnIndex));
		}
		return values;
	}

	public static ContentValues toContentValuesExcepting(Cursor cursor, String... exceptedColumnNames) {
		ContentValues values = new ContentValues(cursor.getColumnCount());
		List<String> columnsAsList = Arrays.asList(exceptedColumnNames);
		for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
			if (columnsAsList.contains(cursor.getColumnName(columnIndex))) {
				//skip coping
			} else {
				values.put(cursor.getColumnName(columnIndex), cursor.getString(columnIndex));
			}
		}
		return values;
	}

	public static void dump(ContentValues values) {
		if (values != null) {
			StringBuilder dump = new StringBuilder();
			for (Entry<String, Object> value : values.valueSet()) {
				dump.setLength(0);
				dump.append(value.getKey()).append(": ").append(value.getValue()).append("\n");
				Log.v("DUMP", dump.toString());
			}
		} else {
			Log.v("DUMP", "null");
		}
	}

	public static boolean equalsInColumn(ContentValues values1, ContentValues value2, String colunm) {
		Object object1 = values1.get(colunm);
		Object object2 = value2.get(colunm);
		return object1 == null && object2 == null || object1.equals(object2);
	}

	public static String getString(Cursor cursor, String colunmName) {
		return cursor.getString(cursor.getColumnIndex(colunmName));
	}

	public static int getInt(Cursor cursor, String colunmName) {
		return cursor.getInt(cursor.getColumnIndex(colunmName));
	}

	public static short getShort(Cursor cursor, String colunmName) {
		return cursor.getShort(cursor.getColumnIndex(colunmName));
	}

	public static float getFloat(Cursor cursor, String colunmName) {
		return cursor.getFloat(cursor.getColumnIndex(colunmName));
	}

	public static boolean equalsExcepting(ContentValues values1, ContentValues values2, String... exceptedColumnNames) {
		ContentValues values1Clone = new ContentValues(values1);
		for (String column : exceptedColumnNames) {
			values1Clone.remove(column);
		}

		ContentValues values2Clone = new ContentValues(values2);
		for (String column : exceptedColumnNames) {
			values2Clone.remove(column);
		}

		return values1Clone.equals(values2Clone);
	}

	public static void copyString(Cursor from, ContentValues to, String column) {
		to.put(column, getString(from, column));
	}

	public static void copyInt(Cursor from, ContentValues to, String column) {
		to.put(column, getInt(from, column));
	}

	public static void copyShort(Cursor from, ContentValues to, String column) {
		to.put(column, getShort(from, column));
	}

	public static void copyFloat(Cursor from, ContentValues to, String column) {
		to.put(column, getFloat(from, column));
	}

	public static long getLong(Cursor cursor, String colunmName) {
		return cursor.getLong(cursor.getColumnIndex(colunmName));
	}

}
